import csv
import os
import logging
from typing import Dict, List, Optional

logger = logging.getLogger(__name__)

class HSCodeService:
    def __init__(self):
        self.hscode_data: Dict[str, str] = {}
        self._load_hscode_data()
    
    def _load_hscode_data(self):
        """从CSV文件加载HSCode数据到内存"""
        try:
            # 获取CSV文件路径
            current_dir = os.path.dirname(os.path.abspath(__file__))
            
            # 加载原有的HSCode.csv文件
            csv_path = os.path.join(current_dir, '..', 'config', 'HSCode.csv')
            csv_path = os.path.normpath(csv_path)
            
            if os.path.exists(csv_path):
                # 读取HSCode.csv文件
                with open(csv_path, 'r', encoding='utf-8') as file:
                    reader = csv.DictReader(file)
                    for row in reader:
                        hscode = row.get('HSCode', '').strip()
                        chinese_desc = row.get('ChineseDescription', '').strip()
                        
                        if hscode and chinese_desc:
                            self.hscode_data[hscode] = chinese_desc
                logger.info(f"成功加载HSCode.csv: {len(self.hscode_data)} 条数据")
            else:
                logger.warning(f"HSCode.csv文件不存在: {csv_path}")
            
            # 加载新的HSCode_10.csv文件
            csv_10_path = os.path.join(current_dir, '..', 'config', 'HSCode_10.csv')
            csv_10_path = os.path.normpath(csv_10_path)
            
            if os.path.exists(csv_10_path):
                # 读取HSCode_10.csv文件
                with open(csv_10_path, 'r', encoding='utf-8') as file:
                    reader = csv.DictReader(file)
                    count_before = len(self.hscode_data)
                    for row in reader:
                        hscode = row.get('编码', '').strip()  # HSCode_10.csv使用'编码'字段
                        chinese_desc = row.get('名称', '').strip()  # HSCode_10.csv使用'名称'字段
                        
                        if hscode and chinese_desc:
                            self.hscode_data[hscode] = chinese_desc
                    count_after = len(self.hscode_data)
                    logger.info(f"成功加载HSCode_10.csv: 新增 {count_after - count_before} 条数据")
            else:
                logger.warning(f"HSCode_10.csv文件不存在: {csv_10_path}")
            
            logger.info(f"总计加载 {len(self.hscode_data)} 条HSCode数据")
            
        except Exception as e:
            logger.error(f"加载HSCode数据失败: {str(e)}", exc_info=True)
    
    def get_multiple_descriptions(self, hscodes: List[str]) -> Dict[str, str]:
        """批量获取多个HSCode的中文描述"""
        result = {}
        for hscode in hscodes:
            if hscode and str(hscode).strip():
                clean_hscode = str(hscode).strip()
                chinese_desc = self.hscode_data.get(clean_hscode)
                if chinese_desc:
                    result[clean_hscode] = chinese_desc
        return result

# 创建全局实例
hscode_service = HSCodeService()